package zero.tech.games.logic.work.room;

import org.springframework.util.Assert;

import zero.tech.core.ZeroContext;
import zero.tech.core.data.Resp;
import zero.tech.core.data.RespCode;
import zero.tech.core.event.service.api.EventService;
import zero.tech.core.work.QueueWork;
import zero.tech.core.work.WorkQueue;
import zero.tech.games.common.Code;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.LogicEventType;
import zero.tech.games.logic.data.consts.LOGICCMD;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.LogicUtil;
import zero.tech.wsnet.controller.LogicController;
import zero.tech.wsnet.data.Message;

public class RecoveryRoomInit extends QueueWork{
	private Room room;
	private LogicController online;
	
	@Override
	public void init(Object... objs) throws Exception {
		room=(Room) objs[0];
		online=(LogicController) objs[1];
	}

	@Override
	public void run() {
		int uid=online.getId();
		if(!room.getAllControllers().containsKey(uid)) {
			online.getWebClient().sendMessage(Message.build(LOGICCMD.RES_ROOMINFO, Resp.build(Code.ROOM_NOT_EXSIST, "已不在房间中")));
			return;
		}
		String roomId=room.getRoom_id();
		LogicController oldonline=room.getAllControllers().get(uid);
		Assert.notNull(oldonline, "此时oldonline一定不为null");
		online.setWin(oldonline.getWin());
		online.setGold(oldonline.getGold());
		online.setGold_from_gate(oldonline.getGold_from_gate());
		online.setRoomId(roomId);
		online.setRole_id(oldonline.getRole_id());
		room.getAllControllers().put(uid, online);
		LogicUtil.someoneOnline(room, online);
		EventService eventService=ZeroContext.getInstance(EventService.class);
		ResReconnet resreconnect = eventService.engineCallBack(LogicEventType.ROOM_RECONNECT, room,online);
		online.getWebClient().sendMessage(Message.build(LOGICCMD.RES_ROOMINFO, Resp.build(RespCode.OK, "恢复场景获取房间信息成功", resreconnect)));
	}

	@Override
	public WorkQueue getWorkQueue() {
		return new WorkQueue() {
			
			@Override
			public long getId() {
				return room.getQueue_id();
			}
		};
	}

}
